import os

import DataExtraction.BSAIndexEntry

class BSAVOC:
    '''Class that represents a VOC file in GLOBAL.BSA'''
    def __init__(self, entry = None, global_bsa_data = None):
        self._entry = entry
        self._global_bsa_data = global_bsa_data
        self._data = None
        
    def __repr__(self):
        return 'BSAVOC: [entry=' + self._entry.__repr__() + ']'
    
    # getters
    def get_entry(self):
        return self._entry
    
    def get_global_bsa_data(self):
        return self._global_bsa_data
    
    def get_data(self):
        return self._data
    
    # setters
    def set_entry(self, entry):
        self._entry = entry
        
    def set_global_bsa_data(self, global_bsa_data):
        self._global_bsa_data = global_bsa_data
        
    def set_data(self, data):
        self._data = data
        
    def load(self):
        # seek to offset
        self._global_bsa_data.seek(self._entry.get_offset(), os.SEEK_SET)
        
        # read X data bytes
        self._data = self._global_bsa_data.read(self._entry.get_size())
        
    def dump(self, filename):
        if not os.path.exists(filename):
            f = open(filename, 'wb')
            
            if f:
                f.write(self._data)
            
            f.close()
        else:
            print 'Skipping already existing file: ', filename